package com.afinal.intelligencetrash.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.text.TextUtils;
import android.view.View;

import com.afinal.intelligencetrash.Presenter.Interinfaces.ActionCallBack;
import com.afinal.intelligencetrash.Presenter.Interinfaces.RegisterService;
import com.afinal.intelligencetrash.Presenter.RegisterServiceImp;
import com.afinal.intelligencetrash.R;
import com.afinal.intelligencetrash.dagger.component.DaggerActivityComponent;
import com.afinal.intelligencetrash.dagger.module.ActivityModule;
import com.afinal.intelligencetrash.global.CustomApplication;
import com.afinal.intelligencetrash.ui.base.AbstractBaseToolbarCoreActivity;
import com.afinal.intelligencetrash.ui.dialog.WaitingDialog;
import com.afinal.intelligencetrash.ui.interfaces.RegisterView;
import com.afinal.intelligencetrash.utils.SimpleUtil;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;


public class RegisterActivity extends AbstractBaseToolbarCoreActivity implements RegisterView{

    @BindView(R.id.et_username) AppCompatEditText et_username;
    @BindView(R.id.et_pwd) AppCompatEditText et_pwd;
    @BindView(R.id.et_pwd2) AppCompatEditText et_pwd2;
    @BindView(R.id.btn_register) AppCompatButton  btn_register;
    private WaitingDialog dialog;
    @Inject
    RegisterServiceImp service;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        ActivityModule.getInstance().addActivity(getClass(), this);
        ((CustomApplication)getApplication()).getActivityComponent().inject(this);
        btn_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.btn_register:{
                registerAction();
            } break;
        }
    }


    private String username;
    private String pwd;
    private void registerAction() {
        username = et_username.getText().toString().trim();
        pwd = et_pwd.getText().toString().trim();
        String pwd2 = et_pwd2.getText().toString().trim();
        if(TextUtils.isEmpty(username)||
                TextUtils.isEmpty(pwd)||
                TextUtils.isEmpty(pwd2)){
            SimpleUtil.showToast(this, "请填写完整");
            return;
        }
        if(!pwd.equals(pwd2)){
            SimpleUtil.showToast(this, "两次密码不一致");
            return;
        }
        service.register(username, pwd, new ActionCallBack() {
            @Override
            public void before() {
                OnRegister();
            }

            @Override
            public void after(final boolean isSuccess) {
                int delay = SimpleUtil.generateRadomNum(500, 2000);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        OnRegisterEnd(isSuccess);
                    }
                }, delay);
            }
        });
    }

    private void OnRegisterEnd(boolean isSuccess) {
        dismissDialog();
        if(isSuccess){
            SimpleUtil.showToast(this, "注册成功");
            Intent intent = new Intent();
            intent.putExtra("username", username);
            intent.putExtra("pwd", pwd);
            this.setResult(REGISTER_SUCCESS, intent);
            finish();
        }
    }

    private void OnRegister() {
        showWiatingDalig("注册中...");
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_register;
    }

    @Override
    protected String getToolbarTitle() {
        return "欢迎注册";
    }

    @Override
    protected boolean isDisplayHomeAsUpEnabled() {
        return true;
    }

    @Override
    protected void dealloc() {

    }



    @Override
    public void showWiatingDalig(String text){
        if(dialog == null){
            dialog = new WaitingDialog.Builder()
                    .setCancelable(true)
                    .build(RegisterActivity.this);
        }
        dialog.setTextView(text);
        dialog.show();
    }

    @Override
    public void dismissDialog(){
        if(dialog != null){
            dialog.dismiss();
        }
    }

    public static void launchActivity(Context context){
        Intent intent = new Intent(context, RegisterActivity.class);
        context.startActivity(intent);
    }

    public static void launchActivityForResult(AppCompatActivity context, int requestCode){
        Intent intent = new Intent(context, RegisterActivity.class);
        context.startActivityForResult(intent, requestCode);
    }

    @Override
    public void showToast(String text) {
        SimpleUtil.showToast(this, text);
    }
}
